<?php
/**
 * 注册
 */
class RegisterController extends AppController {
	public $components = array (
			'RequestHandler' 
	);
	
	/**
	 * 初始化执行
	 *
	 * @see AppController::beforeFilter()
	 */
	public function beforeFilter() {
		parent::beforeFilter ();
	}
	public function index() {
		if ($this->request->isPost ()) {
			$this->loadModel ( "User" );
			
			$data ['User'] ['email'] = $this->data ['email'];
			if (empty ( $data ['User'] ['email'] )) {
				$this->flash ( '请输入邮箱!', '/register/', 1250 );
				return false;
			}
			
			if ($this->User->GetEmailByID ( $this->data ['email'] )) {
				$this->flash ( '邮箱已注册!', '/register/', 1250 );
				return false;
			}
			
			$data ['User'] ['nickname'] = $this->data ['nickname'];
			if (empty ( $data ['User'] ['nickname'] )) {
				$this->flash ( '请输入昵称!', '/register/', 1250 );
				return false;
			}
			
			if ($this->data ['password'] != $this->data ['confirm_password']) {
				$this->flash ( '两次输入的密码不一致!', '/register/', 1250 );
				return false;
			}
			
			$data ['User'] ['password'] = md5 ( $this->data ['confirm_password'] );
			if (empty ( $data ['User'] ['password'] )) {
				$this->flash ( '请输入密码!', '/register/', 1250 );
				return false;
			}
			
			$data ['User'] ['sex'] = 0;
			$data ['User'] ['qq'] = '';
			$data ['User'] ['regip'] = $this->RequestHandler->getClientIP ();
			$data ['User'] ['lastip'] = $this->RequestHandler->getClientIP ();
			$data ['User'] ['ctime'] = date ( "Y-m-d H:i:s", time () );
			
			$UserInfo = $this->User->add ( $data );
			
			if ($UserInfo) {
				$this->flash ( '注册成功!', '/login/', 1250 );
				return true;
			} else {
				$this->flash ( '系统错误，注册失败!', '/register/', 1250 );
				return false;
			}
		} else {
			// 加载css文件
			$this->set ( 'cssList', array (
					'reg.css' 
			) );
			
			// 加载js文件
			$this->set ( 'scriptList', array (
					'register.js' 
			) );
			
			$this->set ( 'title_for_layout', '注册 - 女人集市' );
		}
	}
	
	/**
	 * 检测Email
	 */
	public function email() {
		$email = $this->data ['email'];
		if (empty ( $email )) {
			$msg = array (
					'rtn_code' => - 1,
					'content' => '邮箱不能为空!' 
			);
			echo json_encode ( $msg );
			exit ();
		}
		
		$this->loadModel ( "User" );
		$count = $this->User->find ( 'count', array (
				'conditions' => array (
						'email' => $email 
				) 
		) );
		
		if ($count > 0) {
			$msg = array (
					'rtn_code' => - 1,
					'content' => '邮箱已注册!' 
			);
			echo json_encode ( $msg );
			exit ();
		}
		
		$msg = array (
				'rtn_code' => 0,
				'content' => '邮箱可以注册!' 
		);
		echo json_encode ( $msg );
		exit ();
	}
}
?>